home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #4 / Amiga Plus CD - 2000 - No. 4.iso / Tools / Misc / bgui / Examples / Source / Useless.c < prev    next >
C/C++ Source or Header  |  2000-05-09  |  5KB  |  233 lines

  1. /*
  2.  * @(#) $Header: /cvsroot/bgui/examples/Useless.c,v 41.11 2000/05/09 20:34:00 mlemos Exp $
  3.  *
  4.  * Useless.c
  5.  *
  6.  * (C) Copyright 1998 Manuel Lemos.
  7.  * (C) Copyright 1995-1996 Jaba Development.
  8.  * (C) Copyright 1995-1996 Jan van den Baard.
  9.  * All Rights Reserved.
  10.  *
  11.  * This is DragNDrop in it's most basic form. It is totally
  12.  * useless unless you create subclasses which actually make
  13.  * use of the dragged and dropped information.
  14.  *
  15.  * This example does show you a way to handle input events
  16.  * in a class which supports dragging.
  17.  *
  18.  * The program will open a window with two buttons, one draggable
  19.  * and one droppable. You can drag the draggable object on
  20.  * the dropable and when you release it the screen will beep.
  21.  *
  22.  * $Log: Useless.c,v $
  23.  * Revision 41.11  2000/05/09 20:34:00  mlemos
  24.  * Bumped to revision 41.11
  25.  *
  26.  * Revision 1.2  2000/05/09 19:59:15  mlemos
  27.  * Merged with the branch Manuel_Lemos_fixes.
  28.  *
  29.  * Revision 1.1.2.1  1998/02/28 17:45:45  mlemos
  30.  * Ian sources
  31.  *
  32.  *
  33.  */
  34.  
  35. /* Execute me to compile with DICE V3.0
  36. dcc Useless.c -proto -mi -ms -mRR -lbgui
  37. quit
  38. */
  39.  
  40. #include "DemoCode.h"
  41.  
  42. /*
  43.  *    Simple type-cast.
  44.  */
  45. #define INPUT(x)        (( struct gpInput * )x )
  46.  
  47. /*
  48.  *    We need a simple subclass of the BGUI button class to
  49.  *    make it draggable.
  50.  */
  51. SAVEDS ASM ULONG DispatchSB( REG(a0) Class *cl, REG(a2) Object *obj, REG(a1) Msg msg )
  52. {
  53.     struct RastPort         *rp;
  54.     struct gpInput         copy;
  55.     ULONG             rc = 0L, dr;
  56.  
  57.     switch ( msg->MethodID ) {
  58.  
  59.         case    GM_HANDLEINPUT:
  60.             /*
  61.              *    Handle the input. To implement a draggable object we
  62.              *    need to call the BASE_DRAGGING method as part of our
  63.              *    input-processing code. This method will tell us what
  64.              *    to do with the input event.
  65.              */
  66.  
  67.             /*
  68.              *    Copy the input message. The BASE_DRAGGING method uses
  69.              *    the same message structure.
  70.              */
  71.             copy = *INPUT( msg );
  72.             copy.MethodID = BASE_DRAGGING;
  73.  
  74.             /*
  75.              *    Send this method to ourself. Eventually it will end
  76.              *    up at the baseclass.
  77.              */
  78.             if (( dr = DoMethodA( obj, ( Msg )© )) != BDR_DRAGGING ) {
  79.                 switch ( dr ) {
  80.  
  81.                     case    BDR_DRAGPREPARE:
  82.                         /*
  83.                          *    The baseclass is about to start
  84.                          *    dragging the object. We de-select
  85.                          *    the object.
  86.                          */
  87.                         if ( rp = ObtainGIRPort( INPUT( msg )->gpi_GInfo )) {
  88.                             SetAttrs( obj, GA_Selected, FALSE, TAG_END );
  89.                             DoMethod( obj, GM_RENDER, INPUT( msg )->gpi_GInfo, rp, GREDRAW_REDRAW );
  90.                             ReleaseGIRPort( rp );
  91.                         }
  92.                         break;
  93.  
  94.                     case    BDR_NONE:
  95.                         /*
  96.                          *    The baseclass did not respond to the message
  97.                          *    so we will handle this event by calling the
  98.                          *    superclass (buttonclass).
  99.                          */
  100.                         rc = DoSuperMethodA( cl, obj, msg );
  101.                         break;
  102.  
  103.                     case    BDR_DROP:
  104.                         /*
  105.                          *    Object dropped on the other object.
  106.                          */
  107.                     case    BDR_CANCEL:
  108.                         /*
  109.                          *    Dropped on a non-droppable object or
  110.                          *    operation cancelled. We simply return
  111.                          *    GMR_NOREUSE in these cases.
  112.                          */
  113.                         rc = GMR_NOREUSE;
  114.                         break;
  115.                 }
  116.             } else
  117.                 rc = GMR_MEACTIVE;
  118.             break;
  119.  
  120.         default:
  121.             /*
  122.              *    All the rest goes to the superclass...
  123.              */
  124.             rc = DoSuperMethodA( cl, obj, msg );
  125.             break;
  126.     }
  127.     return( rc );
  128. }
  129.  
  130. /*
  131.  *    Initialize the useless class.
  132.  */
  133. Class *InitUselessClass( void )
  134. {
  135.     Class            *cl = NULL, *super;
  136.  
  137.     /*
  138.      *    Get a pointer to the ButtonClass which
  139.      *    is our superclass.
  140.      */
  141.     if ( super = BGUI_GetClassPtr( BGUI_BUTTON_GADGET )) {
  142.         if ( cl = MakeClass( NULL, NULL, super, 0L, 0L ))
  143.             cl->cl_Dispatcher.h_Entry = ( HOOKFUNC )DispatchSB;
  144.     }
  145.     return( cl );
  146. }
  147.  
  148. /*
  149.  *    Here we go.
  150.  */
  151. VOID StartDemo( void )
  152. {
  153.     struct Window        *window;
  154.     Class            *class;
  155.     Object            *WO_Window;
  156.     ULONG             signal = 0, rc;
  157.     BOOL             running = TRUE;
  158.  
  159.     /*
  160.      *    Setup the class.
  161.      */
  162.     if ( class = InitUselessClass()) {
  163.         /*
  164.          *    Create the window object tree.
  165.          */
  166.         WO_Window = WindowObject,
  167.             WINDOW_Title,        "Useless ;)",
  168.             WINDOW_RMBTrap,         TRUE,
  169.             WINDOW_AutoAspect,    TRUE,
  170.             WINDOW_SizeGadget,    FALSE,
  171.             WINDOW_MasterGroup,
  172.                 VGroupObject, NormalVOffset, NormalHOffset, NormalSpacing,
  173.                     StartMember,
  174.                         InfoFixed( NULL, ISEQ_C "Useless demo. Just drag and drop the\nleft button on the right one", NULL, 2 ), FixMinHeight,
  175.                     EndMember,
  176.                     StartMember,
  177.                         HGroupObject, WideSpacing,
  178.                             StartMember,
  179.                                 /*
  180.                                  *    Create an object from the above
  181.                                  *    defined class.
  182.                                  */
  183.                                 NewObject( class, NULL, FuzzButtonFrame,
  184.                                     Label( "Drag me!" ), BT_DragObject, TRUE, TAG_END ),
  185.                             EndMember,
  186.                             StartMember,
  187.                                 ButtonObject,
  188.                                     FuzzButtonFrame,
  189.                                     Label( "Drop me!" ),
  190.                                     BT_DropObject, TRUE,
  191.                                 EndObject,
  192.                             EndMember,
  193.                         EndObject,
  194.                     EndMember,
  195.                 EndObject,
  196.         EndObject;
  197.  
  198.         /*
  199.          *    Object OK?
  200.          */
  201.         if ( WO_Window ) {
  202.             /*
  203.              *    Open it.
  204.              */
  205.             if ( window = WindowOpen( WO_Window )) {
  206.                 /*
  207.                  *    Get signal mask.
  208.                  */
  209.                 GetAttr( WINDOW_SigMask, WO_Window, &signal );
  210.                 do {
  211.                     Wait( signal );
  212.  
  213.                     /*
  214.                      *    Handle messages.
  215.                      */
  216.                     while (( rc = HandleEvent( WO_Window )) != WMHI_NOMORE ) {
  217.                         switch ( rc ) {
  218.                             case    WMHI_CLOSEWINDOW:
  219.                                 running = FALSE;
  220.                                 break;
  221.                         }
  222.                     }
  223.                 } while ( running );
  224.             } else
  225.                 Tell( "Unable to open the window.\n" );
  226.             DisposeObject( WO_Window );
  227.         } else
  228.             Tell( "Unable to create the window object.\n" );
  229.         FreeClass( class );
  230.     } else
  231.         Tell( "Unable to initialize the class.\n" );
  232. }
  233.